/*
 * 文件名： ChouJiangRecordActivity.java
 * 描    述：我的抽奖历史列表
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.miguo.activity.base.BaseActivity;
import com.miguo.app.AppContext;
import com.miguo.app.AppConfig;
import com.miguo.bean.ChouJiangGoodsBean;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.request.FailData;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.miguo.framework.view.pulltorefresh.PullToRefreshListView;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;
import com.miguo.utils.Options;
import com.miguo.utils.ScoreShopUitl;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ChouJiangRecordActivity extends BaseActivity implements
        OnRefreshListener<ListView> {

    private ArrayList<ChouJiangGoodsBean> list = new ArrayList<ChouJiangGoodsBean>();
    private ScoreShopAdapter adapter;
    private PullToRefreshListView pullToRefreshView;
    private ListView listView;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    protected BaseHeadView mBaseHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_choujiang);
        findViewById();
        initView();
    }

    private void findViewById() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle("我的抽奖");
        pullToRefreshView = (PullToRefreshListView) findViewById(R.id.scoreShop_list);
    }

    private void initView() {
        listView = pullToRefreshView.getRefreshableView();
        Drawable divider = new ColorDrawable(0xdddddddd);
        listView.setDivider(divider);
        listView.setDividerHeight(1);
        adapter = new ScoreShopAdapter();
        listView.setAdapter(adapter);
        pullToRefreshView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshView.setOnRefreshListener(this);
        getData();
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        getData();
    }

    private void getData() {

        AjaxCallBack AjaxCall = new AjaxCallBack(mActivity) {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);

                pullToRefreshView.setVisibility(View.VISIBLE);
                pullToRefreshView.onRefreshComplete();

                if (json.optInt("status") == 200) {
                    JsonData data = json.optJson("data");
                    JsonData item = data.optJson("item");
                    list.clear();
                    list.addAll(ScoreShopUitl.ParserList(item));
                    adapter.notifyDataSetChanged();
                } else {
                    Toast.show(mContext, json.optString("info"));
                }
            }

            @Override
            public void onRequestFail(FailData data) {
                super.onRequestFail(data);
                pullToRefreshView.onRefreshComplete();
                pullToRefreshView.setVisibility(View.GONE);
            }
        };

        Map<String, String> param = new HashMap<String, String>();
        param.put("token", AppContext.UserInfo().getToken());
        HttpClient.postEncryptData(Url.toRoute(Url.Lottery.MyChoujiang, 1), param,
                AjaxCall);
    }

    private class ScoreShopAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private DisplayImageOptions imageOptions;

        public ScoreShopAdapter() {
            inflater = LayoutInflater.from(mActivity);
            imageOptions = Options.getFadeOptions();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public ChouJiangGoodsBean getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_score_shop_lottery, parent, false);
                holder = new ViewHolder();
                holder.img = (ImageView) convertView.findViewById(R.id.lottery_img_iv);
                holder.title = (TextView) convertView.findViewById(R.id.lottery_title_tv);
                holder.score = (TextView) convertView.findViewById(R.id.lottery_score_tv);
                holder.price = (TextView) convertView.findViewById(R.id.lottery_price_tv);
                holder.count = (TextView) convertView.findViewById(R.id.lottery_count_tv);
                holder.attendNormal = (TextView) convertView.findViewById(R.id.lottery_attend_normal_tv);
                holder.attendOver = (TextView) convertView.findViewById(R.id.lottery_attend_over_tv);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            ChouJiangGoodsBean bean = getItem(position);

            imageLoader.displayImage(bean.img, holder.img, imageOptions);
            holder.title.setText(bean.name);

            String src = bean.integral + "积分";
            SpannableString sb = new SpannableString(src);
            sb.setSpan(new AbsoluteSizeSpan(12, true), src.indexOf("积"),
                    src.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.score.setText(sb);

            holder.price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.price.getPaint().setAntiAlias(true);
            holder.price.setText("价格￥" + bean.price);
            holder.count.setText("抽奖人数 " + bean.count);
            if ((System.currentTimeMillis() / 1000) > Long
                    .parseLong(bean.endTime)) {
                holder.attendOver.setVisibility(View.VISIBLE);
                holder.attendNormal.setVisibility(View.GONE);
            } else {
                holder.attendOver.setVisibility(View.GONE);
                holder.attendNormal.setVisibility(View.VISIBLE);
            }

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, ScoreShopDetailLotteryActivity.class);
                    intent.putExtra("bean", list.get(position));
                    startActivity(intent);
                    Statistics.onTrackCustomClick(mContext,
                            AppConfig.STATISTICS.EVENT_NAME_44);
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        TextView title, score, price, count, attendNormal, attendOver;
        ImageView img;
    }

}
